﻿using System;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;

namespace CodeCounsel.SharePoint.NavigationService
{
    [Guid("0bc4db91-dac2-4ad6-90cf-394997131d54")]
    [SupportedServiceApplication("f46c5a6a-0f73-4c1e-b11b-c2ffb70c19f9",
        "1.0.0.0", typeof(NavigationApplicationProxy))]
    public class NavigationServiceProxy
        : SPIisWebServiceProxy, IServiceProxyAdministration
    {
        public NavigationServiceProxy()
        {
        }

        public NavigationServiceProxy(SPFarm farm)
            : base(farm)
        {
        }

        #region [IServiceProxyAdministration]
        public SPServiceApplicationProxy CreateProxy(
            Type serviceApplicationProxyType, 
            string name, Uri serviceApplicationUri, 
            SPServiceProvisioningContext provisioningContext)
        {
            ValidateApplicationProxyType(serviceApplicationProxyType);
            return NavigationApplicationProxy.Create(name, this, serviceApplicationUri, false);
        }

        public SPPersistedTypeDescription GetProxyTypeDescription(
            Type serviceApplicationProxyType)
        {
            ValidateApplicationProxyType(serviceApplicationProxyType);
            return new SPPersistedTypeDescription("Navigation Application Proxy (Demo)",
                "A proxy to a service application that facilitates cross site navigation");
        }

        public Type[] GetProxyTypes()
        {
            return new Type[] { typeof(NavigationApplicationProxy) };
        }

        void ValidateApplicationProxyType(Type serviceApplicationType)
        {
            if (serviceApplicationType != typeof(NavigationApplicationProxy))
            {
                throw new ArgumentException("Application proxy type not supported");
            }
        }
        #endregion
    }
}
